package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import java.util.Arrays;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.Badge;
import top.jnaw.jee.platform.model.Kcgl;
import top.jnaw.jee.platform.model.KcglSteps;
import top.jnaw.jee.platform.model.KcglTime;
import top.jnaw.jee.platform.model.KcglUser;
import top.jnaw.jee.platform.model.Steps;
import top.jnaw.jee.platform.model.Version;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;

public class kcglController extends Controller {

  // 报名
  public void check() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final String openid = Strings.getString(req, "openid", "");
    final String type = Strings.getString(req, "type", "");
    if (id >= 0 && StrKit.notBlank(openid, type)) {
      result = KcglUser.check(id, openid, type);
    }
    renderJson(result.toJson());
  }

  // 修改章节状态
  public void checkSteps() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final int kid = Strings.getInt(req, "kid", Consts.DEFAULT_BAD_ID);
    final String openid = Strings.getString(req, "openid", "");
    final String status = Strings.getString(req, "status", "");
    if (id >= 0 && kid > 0 && StrKit.notBlank(openid, status)) {
      result = KcglSteps.checkSteps(id, kid, openid, status);
    }
    renderJson(result.toJson());
  }

  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final String openid = Strings.getString(req, "openid", "");
    final String type = Strings.getString(req, "type", "");
    final String keyWord = Strings.getString(req, "keyWord", "");
    renderJson(Strings
        .formatJsonDateTime(
            Strings.formatPage(Kcgl.list(page, size, keyWord, openid, type))));
  }

  //获得徽章列表
  public void xzList() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String openid = Strings.getString(req, "openid", "");
    if (StrKit.notBlank(openid)) {
      result = Kcgl.xzList(openid);
    }
    renderJson(result.toJson());
  }

  public void update() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final int fid = Strings.getInt(req, "fid", Consts.DEFAULT_BAD_ID_1);
    final String title = Strings.getString(req, "title", "");
    final String content = Strings.getString(req, "content", "");
    final int hz_id = Strings.getInt(req, "hz_id", Consts.DEFAULT_BAD_ID);
    final String hz_title = Strings.getString(req, "hz_title", "");
    final String hz_content = Strings.getString(req, "hz_content", "");
    final String hz_type = Strings.getString(req, "hz_type", "kcgl");
    JSONArray steps = Strings.getJSONArray(req, "steps", null);
    if (id > 0 && StrKit.notBlank(title)) {
      int bid = -1;
      if (hz_id > 0) { // 先改徽章
        bid = Badge.update(hz_id, fid, hz_title, hz_content, hz_type);
      } else {
        bid = Badge.add(fid, hz_title, hz_content, hz_type);
      }
      if (bid > 0) { // 修改课程
        int kid = Kcgl.update(id, bid, title, content, steps);
        if (kid > 0) {
          // 修改章节
          // 接口传过来的，如果带着id的存下来
          String ids = "";
          for (int i = 0; i < steps.size(); i++) {
            if (!StrKit.notBlank(steps.getJSONObject(i).getString("title"))) {
              continue;
            }
            int step_id = steps.getJSONObject(i).getIntValue("id");
            if (step_id > 0) {
              ids += "*" + step_id + "*";
            }
          }
          // 原有的章节 id
          List<Steps> list_steps = Steps.getListByKid(kid);
          for (int s = 0; s < list_steps.size(); s++) {
            int sid = list_steps.get(s).getInt("id");
            // 不存在id，删除
            String str = "*" + sid + "*";
            int r1 = ids.indexOf(str);
            if (r1 > -1) {
//              Log.d("存在id: " + sid);
            } else {
              Steps.deleteSid(sid);
//              Log.d("不存在id: " + sid);
            }
          }

          for (int i = 0; i < steps.size(); i++) {
            if (!StrKit.notBlank(steps.getJSONObject(i).getString("title"))) {
              continue;
            }
            String step_title = steps.getJSONObject(i).getString("title");
            int step_num = steps.getJSONObject(i).getIntValue("num");
            int step_id = steps.getJSONObject(i).getIntValue("id");
            if (step_id > 0) {
              result = Steps.update(step_id, step_title, step_num);
            } else {
              result = Steps.add(kid, step_title, step_num);
            }
          }
        }
      }
    }
    renderJson(result.toJson());
  }

  public void add() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int fid = Strings.getInt(req, "fid", Consts.DEFAULT_BAD_ID_1);
    final String title = Strings.getString(req, "title", "");
    final String content = Strings.getString(req, "content", "");
    final String hz_title = Strings.getString(req, "hz_title", "");
    final String hz_content = Strings.getString(req, "hz_content", "");
    final String hz_type = Strings.getString(req, "hz_type", "kcgl");
    JSONArray steps = Strings.getJSONArray(req, "steps", null);

    if (StrKit.notBlank(title, hz_title)) {
      int bid = Badge.add(fid, hz_title, hz_content, hz_type);
      if (bid > 0) {
        int kid = Kcgl.add(bid, title, content, steps);
        if (kid > 0) {
          for (int i = 0; i < steps.size(); i++) {
            if (!StrKit.notBlank(steps.getJSONObject(i).getString("title"))) {
              continue;
            }
            String step_title = steps.getJSONObject(i).getString("title");
            int step_num = steps.getJSONObject(i).getIntValue("num");
            result = Steps.add(kid, step_title, step_num);
          }
        }
      }
    }

    renderJson(result.toJson());
  }

  public void del() {
    ModelResult result = new ModelResult(Result.kModelDeleteError);
    JSONObject req = Strings.toJSONObject(this);
    JSONArray ids = Strings.getJSONArray(req, "ids", null);
    if (null != ids && ids.size() > 0) {
      for (int i = 0; i < ids.size(); i++) {
        if (Kcgl.delete(ids.getInteger(i))) {
          Steps.delete(ids.getInteger(i));
          result.setResult(Result.kSuccess);
        }
      }
    }
    renderJson(result.toJson());
  }

  public void query() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final String openid = Strings.getString(req, "openid", "");
    if (id > 0) {
      result = Kcgl.query(id, openid);
    }
    renderJson(result.toJson());
  }

  // 修改课时
  public void updateTime() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int kid = Strings.getInt(req, "kid", Consts.DEFAULT_BAD_ID);
    final String openid = Strings.getString(req, "openid", "");
    final String time = Strings.getString(req, "time", "0");
    float t = Float.parseFloat(time);
    if (kid >= 0 && StrKit.notBlank(openid)) {
      result = KcglTime.update(kid, openid, t);
    }
    renderJson(result.toJson());
  }

}
